﻿Imports System.Activities
Imports System.ComponentModel
Imports System.Activities.Presentation.Metadata
Imports System.Drawing
Imports System.Activities.Presentation.PropertyEditing

Public Class FolderExistsCheckActivity
    Inherits AsyncCodeActivity
    Implements IRegisterMetadata

    Public Property TargetFolder As InArgument(Of String)
    Public Property Result As OutArgument(Of Boolean)

    Private Delegate Function asyncExistsFolderDelegate(ByVal source As String) As Boolean

    Public Sub New()
        Me.DisplayName = My.Resources.FolderExistsCheckDisplay
    End Sub

    Protected Overrides Function BeginExecute(context As System.Activities.AsyncCodeActivityContext, callback As System.AsyncCallback, state As Object) As System.IAsyncResult
        Dim asyncExecute = New asyncExistsFolderDelegate(AddressOf CheckExistsFolder)
        Dim sourceFolder = context.GetValue(Me.TargetFolder)
        context.UserState = asyncExecute

        Return asyncExecute.BeginInvoke(sourceFolder, callback, state)
    End Function

    Protected Overrides Sub EndExecute(context As System.Activities.AsyncCodeActivityContext, result As System.IAsyncResult)
        Dim asyncExecute = TryCast(context.UserState, asyncExistsFolderDelegate)
        Dim copyResult = asyncExecute.EndInvoke(result)

        context.SetValue(Me.Result, copyResult)
    End Sub

    Private Function CheckExistsFolder(ByVal source As String) As Boolean
        Return System.IO.Directory.Exists(source)
    End Function

    Public Sub Register() Implements System.Activities.Presentation.Metadata.IRegisterMetadata.Register
        Dim build As New AttributeTableBuilder

        'FolderRename Activity
        build.AddCustomAttributes(GetType(FolderExistsCheckActivity),
                                              New ToolboxBitmapAttribute(GetType(FolderExistsCheckActivity), "FolderExistsCheck.png"))
        build.AddCustomAttributes(GetType(FolderExistsCheckActivity), "TargetFolder",
                                              New RequiredArgumentAttribute,
                                              New EditorAttribute(GetType(FolderpathPropertyEditor), GetType(DialogPropertyValueEditor)),
                                              New CategoryAttribute(My.Resources.FolderExistsCheckCategory),
                                              New DisplayNameAttribute(My.Resources.FolderExistsCheckTargetFolderDisplay),
                                              New DescriptionAttribute(My.Resources.FolderExistsCheckTargetFolderDescription))
        build.AddCustomAttributes(GetType(FolderExistsCheckActivity), "Result",
                                              New CategoryAttribute(My.Resources.FolderExistsCheckCategory),
                                              New DisplayNameAttribute(My.Resources.FolderExistsCheckResultDisplay),
                                              New DescriptionAttribute(My.Resources.FolderExistsCheckResultDescription))

        MetadataStore.AddAttributeTable(build.CreateTable)
    End Sub
End Class
